
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tama_Chan.GameEngine_ns;
using Tama_Chan.Utils;


namespace Tama_Chan.Entities_ns
{
   public class Player : GameEntity
   {

      public Player(GameEngine gameEngine, Vector2 position)
      {
         GameEngine = gameEngine;
         Position = position;
			Texture = GameEngine.CircleTexture;
         Color = Color.Yellow;
         Depth = GameEngine.PlayerDepth;
         Speed = GameEngine.PlayerSpeed;
         SoundOne = GameEngine.OofSoundEffect.CreateInstance();
         SoundTwo = GameEngine.HitWallSoundEffect.CreateInstance();
         Velocity = GameEngine.Game1.AccelerometerDelta;
         SetHeading();
         Nose = new SeekPoint {Position = GetNosePosition()};
         PerpendicularClockwise = new SeekPoint {Position = GetPerpendicularClockwisePosition()};
         PerpendicularCounterClockwise = new SeekPoint {Position = GetPerpendicularCounterClockwise()};
         Rotation = 0f;
         RotationRate = MathHelper.TwoPi;
			// Todo: Bounds needs to be set in constructor of all entitys and not in the base class update
         Origin = new Vector2(Texture.Width / 2f, Texture.Height / 2f);
      }


      public override void Update(GameTime gameTime)
      {
         Velocity = GameEngine.Game1.AccelerometerDelta; // If the Accelerometer is not moving it's Nan returns come back as zero now.
         Position += Velocity * Speed; // If velocity is zero this is stil fine.
         SetHeading(); 
         Nose.Position = GetNosePosition();
         PerpendicularClockwise.Position = GetPerpendicularClockwisePosition();
         PerpendicularCounterClockwise.Position = GetPerpendicularCounterClockwise();

         float elapsedSeconds = GeneralUtils.GetSecondsFromTicks(gameTime.ElapsedGameTime.Ticks);
         float rotationDelta = RotationRate * elapsedSeconds;
         float updatedRotation = Rotation + rotationDelta;
         Rotation = MathHelper.WrapAngle(updatedRotation);

         base.Update(gameTime);
      }


      public override void Draw()
      {
         GameEngine.Game1.SpriteBatch.Draw(Texture, Position, null, Color, Rotation, Origin, Vector2.One, SpriteEffects.None, Depth);
         base.Draw();
      }


      private void SetHeading()
      {
         // If the accelerometer isn't moving don't set the heading. 
         // Player is still pointing in the same direction.
         if(Velocity != Vector2.Zero)
         {
            Heading = Vector2.Normalize(Velocity);
         }
      }


      private Vector2 GetNosePosition()
      {
         return Position + Heading * 50f;
      }


      private Vector2 GetPerpendicularClockwisePosition()
      {
         return Position + TransformUtils.GetPerpendicular(Heading, true) * 50f;
      }


      private Vector2 GetPerpendicularCounterClockwise()
      {
         return Position + TransformUtils.GetPerpendicular(Heading, false) * 50f;
      }

   }
}
